package com.jackrain.nea.wms.web.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.core.query.ColumnLink;
import com.jackrain.nea.core.query.QueryException;
import com.jackrain.nea.core.redis.CusRedisTemplate;
import com.jackrain.nea.core.schema.TableManager;
import com.jackrain.nea.util.RedisOpsUtil;
import com.jackrain.nea.web.LoadUserenvCmd;
import com.jackrain.nea.web.QueryCmd;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.query.QueryEngine;
import com.jackrain.nea.web.query.QueryRequestImpl;
import com.jackrain.nea.web.query.QuerySessionImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Slf4j
@Component("LoadUserenvCmdImpl-wms")
@Service(protocol = {"dubbo"}, validation = "true", version = "100", group = "wms")
public class LoadUserenvCmdImpl implements LoadUserenvCmd {

    @Reference(group = "ad",version = "1")
    private QueryCmd queryCmd;

    public HashMap getUserenv(User usr) throws Exception {
        List rs = getUserattr();
        HashMap envs = new HashMap();
        CusRedisTemplate<String, TableManager> redisTemplate = RedisOpsUtil.getObjRedisTemplate();
        TableManager tm = (TableManager)redisTemplate.opsForValue().get("client" + usr.getClientId());

        for(int i = 0; i < rs.size(); ++i) {
            String name = (String)((HashMap)rs.get(i)).get("name");
            String value = (String)((HashMap)rs.get(i)).get("value");

            ColumnLink cl;
            try {
                cl = new ColumnLink(value, tm);
            } catch (Exception var14) {
                throw new QueryException("Could not load " + value + " for " + name + ":" + var14);
            }

            QueryRequestImpl quest = new com.jackrain.nea.core.db.QueryRequestImpl(new QuerySessionImpl(usr));
            quest.setMainTable(cl.getColumns()[0].getTable().getId());
            quest.addSelection(cl.getColumnIDs(), false, (String)null);
            quest.addParam(cl.getColumns()[0].getTable().getPrimaryKey().getId(), usr.getId() + "");
            String sql = quest.toSQL();
            log.debug("Loading variable:" + name + "::" + sql);
            List rs2 = QueryEngine.getInstance().doQueryList(sql);
            if (rs2.size() > 0) {
                for(int j = 0; j < rs2.size(); ++j) {
                    envs.put(name, rs2.get(j));
                    log.debug("Load attribute " + name + "=" + rs2.get(j));
                }
            } else {
                log.error("Could not load variable for attribute name:" + name);
            }
        }

        return envs;
    }

    private List getUserattr(){
        HashMap<String,Object> hashMap = new HashMap();
        hashMap.put("tableName","ad_user_attr");
        hashMap.put("fields", "name,value");
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.put("isactive","Y");
        hashMap.put("whereKeys",jsonObject);
        hashMap.put("type", List.class);
        List result = (List)queryCmd.queryTable(hashMap);
        return result;
    }


}
